﻿using System;
using System.Text.RegularExpressions;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using VioletDevelopmentLaboratory.Net.Web.Component;

namespace VioletDevelopmentLaboratory.Net.Web.WebControls
{
	public abstract class BoardSummaryBase : Control, INamingContainer
	{
		protected Panel parentPanel;

		protected string removeHtmlTags(string content)
		{
			Regex tagRegex = new Regex("<(.|\n)+?>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
			return tagRegex.Replace(content, "");
		}

		protected BoardSummaryBase()
		{
			this.PageSize = 0;
			this.ItemLength = 0;
			this.DateFormat = "";
			this.ReduceCharacter = "";
			this.EmptyDataText = "";
			this.CssClass = "";

			this.HeaderText = "";
			this.HeaderImageUrl = "";

			this.FooterText = "";
			this.FooterImageUrl = "";
		}

		#region Behavior
		[Browsable(true),
		Category("Behavior"),
		DefaultValue(5)]
		public Int32 PageSize { get { return (Int32)ViewState["PageSize"]; } set { ViewState["PageSize"] = value; } }

		[Browsable(true),
		Category("Behavior"),
		DefaultValue(15)]
		public Int32 ItemLength { get { return (Int32)ViewState["ItemLength"]; } set { ViewState["ItemLength"] = value; } }

		[Browsable(true),
		Category("Behavior"),
		DefaultValue("{0:d}&nbsp;")]
		public String DateFormat { get { return ViewState["DateFormat"].ToString(); } set { ViewState["DateFormat"] = value; } }

		[Browsable(true),
		Category("Behavior"),
		DefaultValue("...")]
		public String ReduceCharacter { get { return ViewState["ReduceCharacter"].ToString(); } set { ViewState["ReduceCharacter"] = value; } }

		[Browsable(true),
		Category("Behavior"),
		DefaultValue("No Data")]
		public String EmptyDataText { get { return ViewState["EmptyDataText"].ToString(); } set { ViewState["EmptyDataText"] = value; } }
		#endregion

		#region Appearance
		[Browsable(true),
		Category("Appearance"),
		DefaultValue("")]
		public String CssClass { get { return ViewState["CssClass"].ToString(); } set { ViewState["CssClass"] = value; } }
		#endregion

		#region Header Appearance
		[Browsable(true),
		Category("Appearance"),
		DefaultValue("")]
		public String HeaderText { get { return ViewState["HeaderText"].ToString(); } set { ViewState["HeaderText"] = value; } }

		[Browsable(true),
		Category("Appearance"),
		DefaultValue(""),
		Editor(typeof(System.Web.UI.Design.ImageUrlEditor), typeof(System.Drawing.Design.UITypeEditor)),
		UrlProperty]
		public String HeaderImageUrl { get { return ViewState["HeaderImageUrl"].ToString(); } set { ViewState["HeaderImageUrl"] = value; } }
		#endregion

		#region Footer Appearance
		[Browsable(true),
		Category("Appearance"),
		DefaultValue("")]
		public String FooterText { get { return ViewState["FooterText"].ToString(); } set { ViewState["FooterText"] = value; } }

		[Browsable(true),
		Category("Appearance"),
		DefaultValue(""),
		Editor(typeof(System.Web.UI.Design.ImageUrlEditor), typeof(System.Drawing.Design.UITypeEditor)),
		UrlProperty]
		public String FooterImageUrl { get { return ViewState["FooterImageUrl"].ToString(); } set { ViewState["FooterImageUrl"] = value; } }
		#endregion

		protected override void CreateChildControls()
		{
			this.Controls.Clear();

			this.parentPanel = new Panel();
			this.parentPanel.EnableViewState = false;
			this.parentPanel.CssClass = this.CssClass;

			this.parentPanel.Controls.Add(this.createHeader());

			this.parentPanel.Controls.Add(this.createList());

			this.parentPanel.Controls.Add(this.createFooter());

			this.Controls.Add(this.parentPanel);
		}

		protected abstract Control createHeader();

		protected abstract Control createList();

		protected abstract Control createFooter();

		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);

			if (this.Page.Header.FindControl("BoardSummaryStyleSheet") == null)
			{
				Literal styleLiteral = new Literal();
				styleLiteral.ID = "BoardSummaryStyleSheet";
				styleLiteral.Text = "<!-- BoardSummaryStylesheet //--><link rel=\"stylesheet\" type=\"text/css\" href=\"" + this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "VioletDevelopmentLaboratory.Net.Web.WebResources.BoardSummaryStylesheet.css").Replace("&", "&amp;") + "\" />\r\n";

				this.Page.Header.Controls.Add(styleLiteral);
			}
		}
	}
}
